package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallCompanyMemberMapper;
import org.linlinjava.litemall.db.domain.LitemallCompany;
import org.linlinjava.litemall.db.domain.LitemallCompanyMember;
import org.linlinjava.litemall.db.domain.LitemallCompanyMemberExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class LitemallCompanyMemberService {
    @Resource
    private LitemallCompanyMemberMapper companyMemberMapper;
    @Autowired
    private LitemallCompanyService companyService;

    public List<LitemallCompanyMember> queryIndex() {
        LitemallCompanyMemberExample example = new LitemallCompanyMemberExample();
        example.or().andDeletedEqualTo(false);
        return companyMemberMapper.selectByExample(example);
    }

    public List<LitemallCompanyMember> querySelective(String companyName, Integer page, Integer limit, String sort, String order) {
        LitemallCompanyMemberExample example = new LitemallCompanyMemberExample();
        LitemallCompanyMemberExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isEmpty(companyName)) {
            criteria.andCompanyNameLike(companyName);
        }
        criteria.andDeletedEqualTo(false);
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            example.setOrderByClause(sort + " " + order);
        }

        PageHelper.startPage(page, limit);
        return companyMemberMapper.selectByExample(example);
    }

    public List<LitemallCompanyMember> queryByTelephone(String telephone) {
        LitemallCompanyMemberExample example = new LitemallCompanyMemberExample();
        LitemallCompanyMemberExample.Criteria criteria = example.createCriteria();

        criteria.andTelephoneEqualTo(telephone).andDeletedEqualTo(false);

        return companyMemberMapper.selectByExample(example);
    }

    public List<LitemallCompanyMember> queryByCompanyId(Integer companyId) {
        LitemallCompanyMemberExample example = new LitemallCompanyMemberExample();
        LitemallCompanyMemberExample.Criteria criteria = example.createCriteria();

        criteria.andCompanyIdEqualTo(companyId).andDeletedEqualTo(false);

        return companyMemberMapper.selectByExample(example);
    }
    public int updateById(LitemallCompanyMember member) {
        member.setUpdateTime(LocalDateTime.now());
        if(StringUtils.isEmpty(member.getCompanyName())){
            LitemallCompany company = companyService.findById(member.getCompanyId());
            if(company != null){
                member.setCompanyName(company.getName());
            }
        }
        return companyMemberMapper.updateByPrimaryKeySelective(member);
    }

    public void deleteById(Integer id) {
        companyMemberMapper.deleteByPrimaryKey(id);
    }

    public void add(LitemallCompanyMember member) {
        member.setAddTime(LocalDateTime.now());
        member.setUpdateTime(LocalDateTime.now());
        if(StringUtils.isEmpty(member.getCompanyName())){
            LitemallCompany company = companyService.findById(member.getCompanyId());
            if(company != null){
                member.setCompanyName(company.getName());
            }
        }
        companyMemberMapper.insertSelective(member);
    }

    public LitemallCompanyMember findById(Integer id) {
        return companyMemberMapper.selectByPrimaryKey(id);
    }
}
